<?php
/**
 * Short description for file
 *
 * Long description for file (if any)...
 *
 * LICENSE: LGPL
 *
 * @category   Sabai
 * @package    Sabai_Request
 * @copyright  Copyright (c) 2006 myWeb Japan (http://www.myweb.ne.jp/)
 * @license    http://opensource.org/licenses/lgpl-license.php GNU LGPL
 * @version    CVS: $Id: Web.php 239 2008-03-07 08:39:10Z mikhail.miguel $
 * @link
 * @since      File available since Release 0.1.1
*/

/**
 * Sabai_Request
 */
require 'Sabai/Request.php';

define('SABAI_REQUEST_WEB_PARTIAL_PARAM', '_partial');

/**
 * Short description for class
 *
 * Long description for class (if any)...
 *
 * @category   Sabai
 * @package    Sabai_Request
 * @copyright  Copyright (c) 2006 myWeb Japan (http://www.myweb.ne.jp/)
 * @author     Kazumi Ono <onokazu@gmail.com>
 * @license    http://opensource.org/licenses/lgpl-license.php GNU LGPL
 * @version    CVS: $Id: Web.php 239 2008-03-07 08:39:10Z mikhail.miguel $
 * @link
 * @since      Class available since Release 0.1.1
 */
class Sabai_Request_Web extends Sabai_Request
{
    var $_previousURI;

    /**
     * Constructor
     *
     * @param array $myGlobals
     * @return Sabai_Request_Web
     */
    function Sabai_Request_Web($myGlobals = array())
    {
        $_REQUEST = array_merge($_GET, $_POST);
        if (get_magic_quotes_gpc()) {
            $_REQUEST = Sabai_Request_Web::stripSlashes($_REQUEST);
            $_COOKIE = Sabai_Request_Web::stripSlashes($_COOKIE);
            $_SERVER = Sabai_Request_Web::stripSlashes($_SERVER);
        }
        $globals = array_merge(array('GLOBALS', '_REQUEST', '_COOKIE', '_ENV', '_FILES', '_SERVER', '_SESSION'), $myGlobals);
        Sabai_Request_Web::filterUserData($_REQUEST, $globals);
        Sabai_Request_Web::filterUserData($_COOKIE, $globals);
        $this->_previousURI = @$_SESSION['Sabai_Request_Web_URI'];
        $_SESSION['Sabai_Request_Web_URI'] = $this->getURI();
    }

    /**
     * @static
     * @param mixed $var
     */
    function stripSlashes($var) {
        if (is_array($var)) {
            return array_map(array('Sabai_Request_Web', 'stripSlashes'), $var);
        } else {
            return stripslashes($var);
        }
    }

    /**
     * @static
     * @param mixed $var
     * @param array $globalKeys
     */
    function filterUserData(&$var, $globalKeys = array()) {
        if (is_array($var)) {
            $var_keys = array_keys($var);
            if (array_intersect($globalKeys, $var_keys)) {
                $var = array();
            } else {
                foreach ($var_keys as $key) {
                    Sabai_Request_Web::filterUserData($var[$key], $globalKeys);
                }
            }
        } else {
            $var = str_replace("\x00", '', $var);
        }
    }

    /**
     * Gets the requested URI
     *
     * @return string
     */
    function getURI()
    {
        static $uri;
        if (!isset($uri)) {
            if (!empty($_SERVER['REQUEST_URI'])) {
                $uri = SABAI_URL_SERVER . $_SERVER['REQUEST_URI'];
            } else {
            	switch (@SABAI_URL_METHOD) {
                    case SABAI_URL_METHOD_PATHINFO:
                        $uri = SABAI_URL_SCRIPT . '/' . trim($this->getRoute(), '/') . '?';
                        break;
                    default:
                        $uri = SABAI_URL_SCRIPT . '?q=' . trim($this->getRoute(), '/') . '&';
                        break;
                }
                if (!empty($_SERVER['QUERY_STRING'])) {
                    $uri .= $_SERVER['QUERY_STRING'];
                } else {
                    $qs = array();
                    foreach ($this->getAll() as $k => $v) {
                        $qs[] = urlencode($k) . '=' . urlencode($v);
                    }
                    $uri .= implode($qs, '&');
                }
            }
        }
        return $uri;
    }

    function getPreviousURI()
    {
        return $this->_previousURI;
    }

    function isPost()
    {
        return strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') == 0;
    }

    function getAll()
    {
        return $_REQUEST;
    }

    function _has($name)
    {
        return array_key_exists($name, $_REQUEST);
    }

    function _get($name)
    {
        return $_REQUEST[$name];
    }

    function _set($name, $value)
    {
        $_REQUEST[$name] = $value;
    }

    function hasCookie($name)
    {
        return isset($_COOKIE[$name]);
    }

    function getCookie($name)
    {
        return $_COOKIE[$name];
    }

    /**
     * Gets the user requested path(route)
     *
     * @return string
     */
    function getRoute()
    {
        switch (@SABAI_URL_METHOD) {
            case SABAI_URL_METHOD_PATHINFO:
                return ($q = $this->getAsStr('q')) ? $q : $_SERVER['PATH_INFO'];
            default:
                return urldecode($this->getAsStr('q'));
        }
    }

    /**
     * Checks if partial content is requested, instead of full-layout content
     *
     * @return bool
     */
    function isPartial()
    {
    	return $this->getAsBool(SABAI_REQUEST_WEB_PARTIAL_PARAM, false);
    }
}